package com.snl.micro.blog.user.controller;

import com.snl.micro.blog.common.action.UserAction;
import com.snl.micro.blog.common.base.schuleThreadPool.CommonPool;
import com.snl.micro.blog.common.param.UserSaveParam;
import com.snl.micro.blog.common.protocol.ResponseResult;
import com.snl.micro.blog.common.query.LoginParam;
import com.snl.micro.blog.common.query.UserQuery;
import com.snl.micro.blog.user.pojo.po.UserInfoPo;
import com.snl.micro.blog.user.pojo.vo.UserVo;
import com.snl.micro.blog.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    private String port;

    @GetMapping("hello")
    public ResponseResult<String> hello() {
        return ResponseResult.success("年后");
    }

    @PostMapping("save")
    public ResponseResult save(UserSaveParam param) {
        log.debug("控制器入参：{}",param);
        userService.saveUser(param);
        return ResponseResult.success(null);
    }

    @PostMapping("login")
    public ResponseResult<UserInfoPo> login(LoginParam param) {
        log.debug("登录参数:{}",param);
        UserInfoPo po = userService.login(param);
        return ResponseResult.success(po);
    }

    @GetMapping("query")
    public ResponseResult<List<UserVo>> query(UserQuery query) {
        log.debug("用户业务查询参数：{}",query);
        List<UserVo> vos = userService.query(query);
        return ResponseResult.success(vos);
    }

    @PostMapping("subscrible/{userId}/{id}")
    public ResponseResult subScrible(
            @PathVariable("userId") Long followerId,
            @PathVariable("id") Long followingId
    )
    {
        log.debug("入参,用户id={},粉丝id={}",followingId,followerId);
        userService.subscrible(followerId,followingId);
        return ResponseResult.success(null);
    }


    @PostMapping("unsubscrible/{userId}/{id}")
    public ResponseResult unSubScrible(
            @PathVariable("userId") Long followerId,
            @PathVariable("id") Long followingId
    )
    {
        log.debug("入参,用户id={},粉丝id={}",followingId,followerId);
        userService.unsubscrible(followerId,followingId);
        return ResponseResult.success(null);
    }

    @GetMapping("action")
    public ResponseResult<? extends BlockingQueue<UserAction>> action() {
        LinkedBlockingQueue<UserAction> userActions = CommonPool.get();
        return ResponseResult.success(userActions);
    }


 }
